#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2023/11/1 17:20
# @Author  : 胡浩浩
# @File    : api_doc_api.py
# @IDE     : PyCharm
# @description :
from apps.models.api_doc_model import ApiDocModel, ApiDoc

from fastapi import APIRouter

api_doc_router = APIRouter()


@api_doc_router.get("/api_doc")
async def get_api_doc():
    api_docs = await ApiDocModel.all()
    docs_list = [[doc.product_name, doc.product_version, doc.doc_name, doc.api_number, doc.tags_json,doc.api_list] for doc in
                 api_docs]
    #
    return docs_list
    # return []


@api_doc_router.post("/api_doc")
async def insert_api_doc(apidoc: ApiDoc):
    print(apidoc.product_name)
    select_api_doc = await ApiDocModel.filter(product_name=apidoc.product_name,
                                              product_version=apidoc.product_version).first()
    if select_api_doc:
        # print(select_api_doc.product_name)
        return {"message": f"产品{apidoc.product_name}对应的{apidoc.product_version}版本的接口文档已存,无法进行新增"}
    else:
        get_api_doc = ApiDocModel(product_name=apidoc.product_name, product_version=apidoc.product_version,
                                  doc_name=apidoc.doc_name, api_number=apidoc.api_number, tags_json=apidoc.tags_json,
                                  api_list=apidoc.api_list)
        await get_api_doc.save()
        return {"message": f"产品{apidoc.product_name}新增成功，请前往模块分组模块维护当前产品的的模块分组数据", "data": apidoc}


@api_doc_router.delete("/api_doc")
async def insert_api_doc(apidoc: ApiDoc):
    print(apidoc.product_name)
    select_api_doc = await ApiDocModel.filter(product_name=apidoc.product_name,
                                              product_version=apidoc.product_version).first()
    if select_api_doc:
        await select_api_doc.delete()
        return {"message": f"产品{apidoc.product_name}对应的{apidoc.product_version}版本的接口文档已删除"}
    else:
        return {"message": f"产品{apidoc.product_name}对应的{apidoc.product_version}版本的接口文档不存在"}
